﻿using Maticsoft.Model;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using UI.Forms;

namespace UI.Forms
{
    public partial class CustomerAdd : Form
    {
        private bool _isEdit = false;
        private Customers _model = null;
        Maticsoft.BLL.Customers CustomerDAL = new Maticsoft.BLL.Customers();
        public CustomerAdd(bool isEdit = false, Customers model = null)
        {
            InitializeComponent();
            _isEdit = isEdit;
            _model = model;
            if (_isEdit && _model != null)
            {
                this.Text = "编辑用户";
                uiSymbolButton1.Text = "保存";
                uiTextBox1.Text = model.CustomerId.ToString();
                uiTextBox2.Text = model.CustomerName;
                uiTextBox3.Text = model.Phone;
                uiTextBox6.Text = model.Email;
                uiTextBox5.Text = model.Address;
                uiTextBox4.Text = model.Balance.ToString();
                uiDatetimePicker1.Value = (DateTime)model.CreateAt;

            }
            else
            {
                this.Text = "添加用户";
                uiSymbolButton1.Text = "添加";
                uiDatetimePicker1.Value = DateTime.Now;
                uiTextBox1.Text = "";
                uiTextBox2.Text = "";
                uiTextBox3.Text = "";
                uiTextBox2.Text = "";
                uiTextBox3.Text = "";
                uiTextBox6.Text = "";
                uiTextBox5.Text = "";
                uiTextBox4.Text = "";
                
            }

        }

        private void CustomerAdd_Load(object sender, EventArgs e)
        {

        }

        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            #region 公共校验
            Regex regAccount = new Regex("^[A-Za-z0-9_]{6,12}$");
            if (!regAccount.IsMatch( uiTextBox2.Text))
            {
                 uiTextBox2.BackColor = Color.Red;
                 uiTextBox2.Text = "账号只能包含字母、数字和下划线，且长度在[6-12]之间";
                //MessageBox.Show("账号只能包含字母、数字和下划线，且长度在[6-12]之间", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // 规律：只能包含字母、数字和下划线，且长度在[6-12]之间
            Regex regPassword = new Regex("^[a-zA-Z0-9]{6,12}$");
            if (!regPassword.IsMatch( uiTextBox3.Text))
            {
                 uiTextBox3.BackColor = Color.Red;
                 uiTextBox3.Text = "密码只能包含英文和数字，且长度[6-12]之间";
                //MessageBox.Show("密码只能包含英文和数字，且长度[6-12]之间", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            #endregion

            if (_isEdit)  // 编辑
            {
                Customers model = new Customers()
                {
                    CustomerId = _model.CustomerId,
                    CustomerName = uiTextBox2.Text,
                    Phone = uiTextBox3.Text,
                    Email = uiTextBox6.Text,
                    Address = uiTextBox5.Text,
                    Balance = decimal.Parse(uiTextBox4.Text),
                    CreateAt = uiDatetimePicker1.Value,

                };

                CustomerDAL.Update(model);
                DialogResult = DialogResult.OK;
            }
            else  // 添加
            {
                Maticsoft.Model.Customers model = new Customers()
                {
                    CustomerId = _model.CustomerId,
                    CustomerName = uiTextBox2.Text,
                    Phone = uiTextBox3.Text,
                    Email = uiTextBox6.Text,
                    Address = uiTextBox5.Text,
                    Balance = decimal.Parse(uiTextBox4.Text),
                    CreateAt = uiDatetimePicker1.Value,
                };
                CustomerDAL.Add(model);
                DialogResult = DialogResult.OK;

            }
        }
    }
}
